package br.com.ceped.util;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Util {

	// recebe uma string e retorna um data
	public static Date stringToDate(String data) {
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		Date date = null;
		try {
			date = new Date(format.parse(data).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String dateToString(Date data) {
		SimpleDateFormat formato_data = new SimpleDateFormat("dd/MM/yyyy");
		String data2 = formato_data.format(data);
		return data2;
	}

	public static Date getData() {
		Date data = new Date();
		return data;
	}

	public static Timestamp getHora() {
		SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss");
		String hora = f.format(System.currentTimeMillis());
		Timestamp t = null;
		try {
			t = new Timestamp(f.parse(hora).getTime());
		} catch (ParseException ex) {
			JOptionPane.showMessageDialog(null, "Erro ao formatar a hora: "
					+ ex);
		}
		return t;
	}

	public static void centralizaForm(JFrame frame) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setBounds((screenSize.width - frame.getWidth()) / 2,
				(screenSize.height - frame.getHeight()) / 2, frame.getWidth(),
				frame.getHeight());
	}

	public static String encryptar(String senha) {
		String sen = "";
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		BigInteger hash = new BigInteger(1, md.digest(senha.getBytes()));
		sen = hash.toString(16);
		return sen;
	}
	
	public static String removerMascara(String texto){		
		String text = ""; 
		for(int i = 0; i < texto.length(); i++){
			if(texto.trim().charAt(i) >= 48 && texto.trim().charAt(i) <= 57){
				text += texto.trim().charAt(i);
			}
		}		
		return text;
	}

}
